/**
验证框架需要当前页面引入Extjs.js
参数:validate='{type:dd,config:{}}'
*/
Ext.QuickTips.init();

Ext.ValidateFramework = function(){
	//private
	
	function initValidateFramework(){
	 
		var existFs = Ext.ValidateFramework.validateFields;
		for (var i = 0 ; i < existFs.length;i++){
			existFs[i].clearInvalid();
			//existFs[i].purgeListeners();
		}
		//
		//获取form 元素
		var el = Ext.select("form",true);
		el.each(function(form){
			/*
			 form 
			*/
			var validateFiles = [];
			var fs = form.select('input:not(.dontValidate),select:not(.dontValidate)',true);
			//给输入字段加入校验
			fs.each(function(field){
				
				var validate = field.getAttributeNS('','validate');
				if (validate){
				var vt = Ext.util.JSON.decode(validate);
				var t = generateValidate(vt,field);
				validateFiles.push(t);
				existFs.push(t)
				}
			});
			
			var reset = form.child("input[type=reset]");
			var submit = form.child("input[type=submit]");
			//存在重置
			if (reset){
				reset.removeAllListeners();
				reset.on('click',function(e/*事件对象*/){
					e.preventDefault();
					for (var i = 0; i < Ext.ValidateFramework.validateFields.length; i ++){
						Ext.ValidateFramework.validateFields[i].clearInvalid();
					}
					var restInputs = form.select('input[type=text]:not(.dontReset),input[type=password]:not(.dontReset),select');
					if (restInputs){
						restInputs.each(function(f){
							if(f.is('select')){
								f.child('option',true).selected=true;
							}
							else {
								f.dom.value='';
							}
						});
						
						
					}
					
				});
			}
			//提交按钮
			var submit = form.child("input[type=submit]");
			if (submit){
				submit.removeAllListeners();
				submit.on('click',function(e){
					var error = false;
					var firstErrorField ;
					for (var i = 0; i < validateFiles.length; i ++){
							if (! validateFiles[i].validate()){
								error = true;
								if (!firstErrorField){
									firstErrorField = validateFiles[i];
								}
							
							}
					}
					if (error){
						e.preventDefault();
						firstErrorField.focus();
						alert('输入有错');
						
					}
				});
			}	
		});
		Ext.ValidateFramework.validateFields = existFs;
	};
	//private 
	function generateValidate(conf,f){
		var type = conf.type;
		var conf = conf.config || {};
		Ext.apply(conf,{applyTo:f,ctCls:'x-form-field-wrap',msgTarget:'side'});
		if (!type) return ;
		
		if(f.is("input[type=password]")){
			Ext.apply(conf,{inputType:'password'});
		}
	    if (type == 'date'){
			var ff = new Ext.form.DateField(conf);
			return ff;
		}
		else if (type == 'time'){
			var ff = new Ext.form.TimeField(conf);
			return ff;
		}
		else if (type == 'required'){
			Ext.apply(conf,{allowBlank :false});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		else if (type == 'email'){
			Ext.apply(conf,{vtype:'email'});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		else if (type == 'url'){
			Ext.apply(conf,{vtype:'url'});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		else if (type == 'int'){
			Ext.apply(conf,{allowDecimals:false});
			var ff = new Ext.form.NumberField(conf);
			return ff;
		}
		else if (type =='float'){
			Ext.apply(conf,{allowDecimals:true});
			var ff = new Ext.form.NumberField(conf);
			return ff;
		}
		else if (type == 'string'){
			Ext.apply(conf,{vtype :'alphanum'});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		else if (type == 'zw'){
			Ext.apply(conf,{regex : /^[\u4e00-\u9fa5]+$/});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
	    
		else if (type == 'noff'){
			Ext.apply(conf,{regex : /^[^`~!@#$%^&*()+=|\\\][\]{\}:;',.<>\/?]*$/});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		else if (type == 'password'){
			Ext.apply(conf,{regex : /^[0-9a-zA-Z]{6,20}$/});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		else if (type == 'mobile'){
			Ext.apply(conf,{regex : /^0{0,1}(13[0-9]?|15[0-9])[0-9]{8}$/});
			var ff = new Ext.form.TextField(conf);
			return ff;
		}
		
	};
	return {validateFields:[],domChanged:initValidateFramework};
	
}();
Ext.onReady(Ext.ValidateFramework.domChanged);



//
